package tool;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class FileTool {

	/**
	 * @param args
	 */

	public static void copyFile(String file1,String file2){
		try{
		  FileInputStream fis=new FileInputStream(file1);
		  FileOutputStream fos=new FileOutputStream(file2);
		  int temp;
		  while((temp=fis.read())!=-1){
		   fos.write(temp);
		  }
		  fis.close();
		  fos.close();
		  System.out.println("从"+file1+"到"+file2);
		}catch(Exception e){
			e.printStackTrace();
		}
		 }
		
		
		 public static void cutRename(String file1,String file2){
		  
		  copyFile(file1, file2);//复制
		   File f=new File(file1);
		   f.delete();//删除源文件，达到重命名的目的
		   System.out.println("成功"+file2);
		   System.out.println(file1);
		 }
	
	
	public static boolean Move(File srcFile, String destPath) {
		// Destination directory
		File dir = new File(destPath);

		// Move file to new directory
		boolean success = srcFile.renameTo(new File(dir, srcFile.getName()));

		return success;
	}
	


	public static boolean Move(String srcFile, String destPath) {
		// File (or directory) to be moved
		File file = new File(srcFile);

		// Destination directory
		File dir = new File(destPath);

		// Move file to new directory
		boolean success = file.renameTo(new File(dir, file.getName()));

		return success;
	}

	public static void Copy(String oldPath, String newPath) throws IOException {
		
		InputStream inStream=null;
		try {
			int bytesum = 0;
			int byteread = 0;
			
			File oldfile = new File(oldPath);
			if (oldfile.exists()) {
				inStream = new FileInputStream(oldPath);
				FileOutputStream fs = new FileOutputStream(newPath);
				byte[] buffer = new byte[1444];
				int length;
				while ((byteread = inStream.read(buffer)) != -1) {
					bytesum += byteread;
					System.out.println(bytesum);
					fs.write(buffer, 0, byteread);
				}
				
			}
		} catch (Exception e) {
			System.out.println("error  ");
			e.printStackTrace();
		}finally{
			inStream.close();
		}
	}

	public static void Copy(File oldfile, String newPath) {
		try {
			int bytesum = 0;
			int byteread = 0;
			// File oldfile = new File(oldPath);
			if (oldfile.exists()) {
				InputStream inStream = new FileInputStream(oldfile);
				FileOutputStream fs = new FileOutputStream(newPath);
				byte[] buffer = new byte[1444];
				while ((byteread = inStream.read(buffer)) != -1) {
					bytesum += byteread;
					System.out.println(bytesum);
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
			}
		} catch (Exception e) {
			System.out.println("error  ");
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
